package repository

import (
	"fmt"
	"hcy-api/structs/tables"
	"lms/global"
)

var KnowledgeDB = new(knowledgeDB)

type knowledgeDB struct {
}

func (r knowledgeDB) FindById(id int64) (m tables.Knowledge, err error) {
	err = global.DB.Where(&tables.Knowledge{Id: id}).Find(&m).Error
	return
}

func (r knowledgeDB) Create(m *tables.Knowledge) error {
	err := global.DB.Create(m).Error
	return err
}

func (r knowledgeDB) DeleteById(id int64) (err error) {
	err = global.DB.Delete(&tables.Knowledge{Id: id}).Error
	return
}

func (r knowledgeDB) List(name string) (li []tables.Knowledge, err error) {
	err = global.DB.Model(&tables.Knowledge{}).Where(" name like ? ", fmt.Sprintf("%%%s%%", name)).
		Find(&li).Error
	return
}

func (r knowledgeDB) BatchBindDoc(li []tables.MapKnowledgeDoc) (err error) {
	err = global.DB.CreateInBatches(&li, len(li)).Error
	return
}

func (r knowledgeDB) BindDoc(m *tables.MapKnowledgeDoc) (err error) {
	err = global.DB.Create(m).Error
	return
}
